<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
            <el-form-item label="材料商名称" prop="materialSupplierName">
                <el-input v-model="queryParams.materialSupplierName" placeholder="请输入材料商名称" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="授权手机号" prop="contactInformation">
                <el-input v-model="queryParams.contactInformation" placeholder="请输入授权手机号" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" size="small" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" size="small" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="materialSupplierList" default-expand-all row-key="materialSupplierId"
            :tree-props="{ children: 'updateRecord' }">
            <el-table-column label="材料商ID" align="center" :show-overflow-tooltip="true" prop="materialSupplierId" />
            <el-table-column label="材料商名称" align="center" :show-overflow-tooltip="true"
                prop="materialSupplierName"></el-table-column>
            <el-table-column label="地址" align="center" :show-overflow-tooltip="true" prop="address"></el-table-column>
            <el-table-column label="授权手机号" align="center" :show-overflow-tooltip="true"
                prop="contactInformation"></el-table-column>
            <el-table-column label="头像" align="center" prop="avatarImg">
                <template #default="scope">
                    <image-preview :width="60" :height="60" :src="scope.row.avatarImg"
                        :preview-src-list="[scope.row.avatarImg]"></image-preview>
                </template>
            </el-table-column>
            <el-table-column label="品牌形象照" align="center" prop="figureImg">
                <template #default="scope">
                    <image-preview :width="60" :height="60" :src="scope.row.figureImg"
                        :preview-src-list="[scope.row.figureImg]"></image-preview>
                </template>
            </el-table-column>
            <el-table-column label="入驻时间" align="center" :show-overflow-tooltip="true" prop="createTime" />
            <el-table-column label="材料商状态" align="center" prop="materialSupplierStatus">
                <template #default="scope">
                    <dict-tag :options="material_supplier_status" :value="scope.row.materialSupplierStatus" />
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Stamp" size="small" v-hasPermi="['decor:materialSupplier:edit']"
                        @click="handleView(scope.row)">审核</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
</template>

<script setup>
import { listMaterialSupplier } from "@/api/decor/materialSupplier";
import { getImgUrl } from "@/api/decor/designer";
const { proxy } = getCurrentInstance();
const { material_supplier_status } = proxy.useDict('material_supplier_status');
const router = useRouter();
// 表格数据
const materialSupplierList = ref([]);
// 遮罩层
const loading = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialSupplierName: null,
        contactInformation: null,
        needAdminSort: false
    },
});

const { queryParams, form } = toRefs(data);

/** 查询材料商列表 */
function getList() {
    loading.value = true;
    listMaterialSupplier(queryParams.value).then(response => {
        let imgUrlPromises = [];
        let data = response.rows.map((v) => {
            return { ...v, figureImg: '', avatarImg: '' }
        })
        data.forEach((v) => {
            // 获取头像的异步操作
            if (v.avatar) {
                let avatarPromise = getImgUrl(v.avatar).then((res) => {
                    v.avatarImg = res.data[0].url;
                });
                imgUrlPromises.push(avatarPromise);
            }
            // 获取形象照的异步操作
            if (v.profilePicture) {
                let coverImagePromise = getImgUrl(v.profilePicture).then((res) => {
                    v.figureImg = res.data[0].url;
                });
                imgUrlPromises.push(coverImagePromise);
            }
            if (v.updateRecord.length > 0) {
                // 获取头像的异步操作
                if (v.updateRecord[0].avatar) {
                    let avatarPromise = getImgUrl(v.updateRecord[0].avatar).then((res) => {
                        v.updateRecord[0].avatarImg = res.data[0].url;
                    });
                    imgUrlPromises.push(avatarPromise);
                }
                // 获取形象照的异步操作
                if (v.updateRecord[0].profilePicture) {
                    let coverImagePromise = getImgUrl(v.updateRecord[0].profilePicture).then((res) => {
                        v.updateRecord[0].figureImg = res.data[0].url;
                    });
                    imgUrlPromises.push(coverImagePromise);
                }
            }
        })
        // 等待所有的异步操作完成
        Promise.all(imgUrlPromises).then(() => {
            // 所有的异步操作完成后再更新
            materialSupplierList.value = data;
            total.value = response.total;
            loading.value = false;
        });

    });


}
/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 审核按钮操作 */
function handleView(row) {
    router.push('/MaterialStore/auditMaterial/' + row.materialSupplierId + '/audit')
}

getList();
</script>
<style lang="scss" scoped>
::v-deep(.el-button--primary.is-link) {
    color: #EF5A42;
}
</style>